/**
 * 
 */
package com.bestbuy.controller;
import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bestbuy.pojo.Account;
import com.bestbuy.pojo.AccountFacebookInfo;
import com.bestbuy.pojo.AccountInfo;
import com.bestbuy.pojo.Catalog;
import com.bestbuy.pojo.Product;
import com.bestbuy.service.AccountService;
import com.bestbuy.service.CatalogService;
import com.bestbuy.service.ProductService;
import com.bestbuy.util.APIWrapper;

/**
 * @author lyhoang
 *
 */
@Controller
public class AccountController 
{
	@Autowired
	private CatalogService catalogService;
	@Autowired
	private ProductService productService;
	@Autowired
	private AccountService accountService;
	
    protected AuthenticationManager authenticationManager;

	
	
	@RequestMapping(value = { "/account.do"})
	public String AccountDetail(Model model) 
	{
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		//List<Product> listProduct = productService.Search("", "0", "1");
		List<Product> listProduct = productService.Search("", "0", "1", "0");
		model.addAttribute("products", listProduct);
		model.addAttribute("Page", "AccountDetail");
		return "Main";
	}
	
	@RequestMapping(value = { "/changepassword.do"})
	public String AccountChangePass(Model model) 
	{
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		//List<Product> listProduct = productService.Search("", "0", "1");
		List<Product> listProduct = productService.Search("", "0", "1", "0");
		model.addAttribute("products", listProduct);
		
		model.addAttribute("Page", "AccountChangePassword");
		return "Main";
	}
	
	@RequestMapping(value = { "/editinformation.do"})
	public String AccountEditinformation(Model model, HttpServletRequest request) 
	{
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		//List<Product> listProduct = productService.Search("", "0", "1");
		List<Product> listProduct = productService.Search("", "0", "1", "0");
		model.addAttribute("products", listProduct);
		
		String username = (String) request.getSession()
				.getAttribute("username");
		// lay thong tin account
		Account account = accountService.GetAccount(username);
		model.addAttribute("account", account);
		
		model.addAttribute("Page", "AccountEditInformation");
		return "Main";
	}
	
	@RequestMapping(value = { "/editinformation.do"}, method = RequestMethod.POST)
	public String Editinformation(Model model, HttpServletRequest request,
			@RequestParam(value="fullname") String fullname,
			@RequestParam(value="birthday") String birthday,
			@RequestParam(value="sex") String sex,
			@RequestParam(value="address") String address,
			@RequestParam(value="email") String email,
			@RequestParam(value="phone") String phone
			) 
	{
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		//List<Product> listProduct = productService.Search("", "0", "1");
		List<Product> listProduct = productService.Search("", "0", "1", "0");
		model.addAttribute("products", listProduct);
		
		String username = (String) request.getSession()
				.getAttribute("username");
		// cap nhat thong tin
		Account account = accountService.UpdateAccountInfo(username, fullname, sex, birthday, address, email, phone);
		model.addAttribute("account", account);
		// truyen qua cau thong bao
		model.addAttribute("done","Cap nhat thanh cong!");
		model.addAttribute("Page", "AccountEditInformation");
		return "Main";
	}
	
	
	
	@RequestMapping(value = { "/viewinformation.do"})
	public String AccountViewinformation(Model model, HttpServletRequest request) 
	{
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		//List<Product> listProduct = productService.Search("", "0", "1");
		List<Product> listProduct = productService.Search("", "0", "1", "0");
		model.addAttribute("products", listProduct);
		String username = (String) request.getSession()
				.getAttribute("username");
		// lay thong tin account
		Account account = accountService.GetAccount(username);
		model.addAttribute("account", account);
		model.addAttribute("Page", "AccountViewInformation");
		return "Main";
	}

}
